package com.biostime.qdingding.modules.qd.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.biostime.qdingding.common.api.FastResponse;
import com.biostime.qdingding.common.api.ListResponse;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.cms.service.ArticleService;
import com.biostime.qdingding.modules.qd.api.dto.ArticleDto;
import com.biostime.qdingding.modules.qd.entity.Album;
import com.biostime.qdingding.modules.qd.service.AlbumService;
import com.github.pagehelper.PageHelper;

/**
 * 育儿干货模块
 * 
 * @author chenweicheng
 * @version 2015-12-21
 */
@RestController
@RequestMapping(value = "${adminPath}/qd/api/babyNews")
public class BabyNewsApi extends BaseController{
	@Autowired
	ArticleService articleService;
	
	@Autowired
	AlbumService albumService;
		
	/**
	 * 获取栏目列表
	 */
	@RequestMapping(value = "getArticleCategory")
	public ListResponse<HashMap<String,Object>> getArticleCategory(String userId) {	
		String message = null;
		int errCode = 0;
		List<HashMap<String,Object>>  mapList = new ArrayList<HashMap<String,Object>>();
	
		if (userId != null && !userId.equals("")) {		
			mapList = articleService.getArticleCategory(String.valueOf(BasicApi.defaultCenterId));
			message = "获取栏目列表成功";
			errCode = 0;
			
		} else {
			message = "获取栏目列表失败，参数有误";
			errCode = 1;
		}
		
		return FastResponse.create().errMsg(message).errCode(errCode).list(mapList);
	}	
	
	/**
	 * 获取文章列表
	 */
	@RequestMapping(value = "getArticles")
	public ListResponse<ArticleDto> getArticles(HttpServletRequest request) {	
		String userId = request.getParameter("userId");
		String categoryId = request.getParameter("categoryId");
		int pageindex = 1;
		int pagesize = 5;	
		String message = null;
		int errCode = 0;	
		List<ArticleDto>  articleList = new ArrayList<ArticleDto>();
	
		if (userId != null && !userId.equals("") && categoryId != null && !categoryId.equals("") && 
				request.getParameter("pageindex") != null &&	request.getParameter("pagesize") != null) {	
			pageindex = Integer.parseInt(request.getParameter("pageindex"));
			pagesize = Integer.parseInt(request.getParameter("pagesize"));
			PageHelper.startPage(pageindex, pagesize);
			
			articleList = articleService.getArticles(categoryId, null);
			//项目url
			String projectUrl = request.getRequestURI();
			String url[] = projectUrl.split("/");
			if(url.length>3){
				projectUrl = url[0]+"/"+url[1]+"/"+url[2];
			}
			for(ArticleDto dto : articleList){
				dto.setHtmlUrl(projectUrl+"/qd/article/show/"+dto.getId());
				List<Album>  albumList = albumService.apiGet("article", String.valueOf(dto.getId()));
				if(albumList !=null && !albumList.isEmpty()){
					dto.setImageUrl(albumList.get(0).getOrginPath()); 
				}
			}
			message = "获取文章列表成功";
			errCode = 0;
			
		} else {
			message = "获取文章列表失败，参数有误";
			errCode = 1;
		}
		
		return FastResponse.create().errMsg(message).errCode(errCode).list(articleList);
	}		
}
